<script setup lang="ts">
import { useGoodsStore } from "@/stores/goodsStore";
import GoodsBread from "./components/GoodsBread.vue";
import GoodsImages from "./components/GoodsImages.vue";
import GoodsSales from "./components/GoodsSales.vue";
import GoodsInfo from "./components/GoodsInfo.vue";
import GoodsSku from "./components/GoodsSku.vue";
import GoodsRelevant from "./components/GoodsRelevant.vue";
import GoodsTab from "./components/GoodsTab.vue";
import GoodsHot from "./components/GoodsHot.vue";
import XtxNumberBox from "../../components/XtxNumberBox.vue";
import XtxButton from "../../components/XtxButton.vue";

// 获取用于存储商品详情信息的 store 对象
const goodsStore = useGoodsStore();
// 获取用于发送请求获取商品详情信息的方法
//
const { goodsInfo } = storeToRefs(goodsStore);
const count = ref(1);
const { getGoodsInfo } = goodsStore;
// 获取路由信息对象
const route = useRoute();
// 获取商品详情信息
getGoodsInfo(route.params.id as string);
// 服务器端发送请求获取商品详细信息
// onBeforeRouteUpdate((to) => {
//   getGoodsInfo(to.params.id as string);
// });
// goodsInfo.value.result.mainPictures;

const skuId = ref<string | undefined>();
</script>
<template>
  <div class="xtx-goods-page">
    <div class="container" v-if="goodsInfo.status === 'success'">
      <!-- 面包屑 -->
      <GoodsBread />
      <!-- 商品信息 -->
      <div class="goods-info">
        <!-- 左侧 -->
        <div class="media">
          <!-- 图片预览组件 -->
          <GoodsImages />
          <!-- 商品销售组件 -->
          <GoodsSales />
        </div>
        <!-- 右侧 -->
        <div class="spec">
          <!-- 商品信息组件 -->
          <GoodsInfo />
          <!-- 规格组件 -->
          <GoodsSku
            :specs="goodsInfo.result.specs"
            :skus="goodsInfo.result.skus"
            @complete="
              goodsStore.updateGoods($event);
              skuId = $event.skuId;
            "
            @unComplete="skuId = undefined"
          />
          <!-- 商品数量选择组件 -->
          <XtxNumberBox
            :max="goodsInfo.result.inventory"
            v-model:count="count"
            label="数量"
          />
          <!-- 统用按钮组件 -->
          <XtxButton type="primary" :style="{ 'margin-top': '20px' }"
            >加入购物车</XtxButton
          >
        </div>
      </div>
      <!-- 同类商品 -->
      <GoodsRelevant :goods-id="goodsInfo.result.id" />
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <GoodsTab />
        </div>
        <!-- 24热榜 -->
        <div class="goods-aside">
          <GoodsHot :id="goodsInfo.result.id" :type="1" />
          <GoodsHot :id="goodsInfo.result.id" :type="2" />
          <GoodsHot :id="goodsInfo.result.id" :type="3" />
        </div>
      </div>
    </div>
    <div
      class="container loading-container"
      v-if="goodsInfo.status === 'loading'"
    >
      <img src="@/assets/images/loading.gif" />
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 600px;
  background: #fff;
}
</style>
